// 判断输入是否为空
export function isEmpty(value) {
	return value == null || value.length === 0
}

// 判断输入是否不为空
export function isNotEmpty(value) {
	return !!value
}

// 判断输入是否是一个整数
export function isInt(value) {
	const regex = /^(-|\+)?\d+$/
	return regex.test(value)
}

export function isPositiveInt(value) {
	const regex = /^\d+$/
	return regex.test(value)
}

export function isIntOrBlank(value) {
	if(value == null) return true
	const str = String(value)
	if(str.trim().length === 0) return true
	const regex = /^(-|\+)?\d+$/
	return regex.test(str)
}

// 数字
export function isNumber(value) {
	const regex = /^-?\d*\.?\d+$/
	return regex.test(value)
}

export function isPositiveNumber(value) {
	const regex = /^\d*\.?\d+$/
	return regex.test(value)
}

export function isNumberOrBlank(value) {
	if(value == null) return true
	const str = String(value)
	if(str.trim().length === 0) return true
	const regex = /^-?\d*\.?\d+$/
	return regex.test(str)
}

// 数字+字母
export function isNumberAndLetter(value) {
	const regex = /^[A-Za-z0-9]+$/
	return regex.test(value)
}

// 数字+字母+下划线+减号
export function isNumberAndLetter2(value) {
	const regex = /^[0-9a-zA-Z_-]+$/
	return regex.test(value)
}

export function isPhone(value) {
	const regex = /^[1]([3-9])[0-9]{9}$/
	return regex.test(value)
}

// 是否是一个固定电话号码
export function isTel(value) {
	const regex = /^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$/
	return regex.test(value)
}

// 是否是一个国际号码
export function isInternationalPhone(value) {
	const regex = /^\s*\+?\s*(\(\s*\d+\s*\)|\d+)(\s*-?\s*(\(\s*\d+\s*\)|\s*\d+\s*))*\s*$/
	return regex.test(value)
}

export function isQq(value) {
	const regex = /^[1-9]\d{4,15}$/i
	return regex.test(value)
}

export function isWx(value) {
	const regex = /^[\d\w\-_]{2,31}$/i
	return regex.test(value)
}

//  判断输入是否是有效的电子邮件
export function isEmail(value) {
	const regex = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
	return regex.test(value)
}

// 是否合法的url
export function isUrl(value) {
	const regex = /^[a-zA-z]+:\/\/[^\s]*$/
	return regex.test(value)
}

// 是否合法的domain
export function isDomain(value) {
	const regex = /^[^\s]*$/
	return regex.test(value)
}

// 是否合法的domainUrl
export function isDomainUrl(value) {
	const regex = /^[\w\-_]+(\.[\w\-_]+)+(:\d+)?$/
	return regex.test(value)
}

export function isHostUrl(value) {
	const regex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
	return regex.test(value)
}

// 是否身份证号码
export function isIdCard(value) {
	const regex = /^\d{15}|\d{}18$/
	return regex.test(value)
}

// 是否是IP
export function isIp(value) {
	const regexIpv4 = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
	const regexIpv6 = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$|^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
	return regexIpv4.test(value) || regexIpv6.test(value)
}

// 是否是IP段
export function isIpSegment(value) {
	const regexIpv4 = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-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}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*(\/\d{1,5})?$/
	return regexIpv4.test(value) || regexIpv6.test(value)
}

export function isDate(value) {
	if(value == null) return false
	value = value.trim()
	if(value.length !== 10) return false
	const regex = /^[1-2]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][1-9]|3[0-1])$/
	const d = new Date(value)
	// @ts-ignore
	return regex.test(value) && d instanceof Date && !isNaN(d)
}

export function isDatetime(value) {
	if(value == null) return false
	value = value.trim()
	if(value.length !== 19) return false
	const regex = /^[1-2]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s\d{2}:\d{2}:\d{2}$/
	const d = new Date(value)
	// @ts-ignore
	return regex.test(value) && d instanceof Date && !isNaN(d)
}

export function isUsername(value) {
	const regex = /^[A-Za-z0-9]{3,15}$/
	return regex.test(value)
}

export function isPassword(value) {
	const regex = /^[A-Za-z0-9]{6,16}$/
	return regex.test(value)
}

export function isCaptcha(value) {
	const regex = /^[A-Za-z0-9]{1,6}$/
	return regex.test(value)
}

export function isCaptcha4(value) {
	const regex = /^[A-Za-z0-9]{4}$/
	return regex.test(value)
}

export function isCaptcha6(value) {
	const regex = /^[A-Za-z0-9]{6}$/
	return regex.test(value)
}

export function isNumberCaptcha4(value) {
	const regex = /^[0-9]{4}$/
	return regex.test(value)
}

export function isNumberCaptcha6(value) {
	const regex = /^[0-9]{6}$/
	return regex.test(value)
}

export function isInvitationCode(value) {
	const regex = /^[0-9]{4,6}$/
	return regex.test(value)
}

export function isGoogleCode(value) {
	const regex = /^\d{6}$/
	return regex.test(value)
}
